package com.timothy.junk.gfund.activity;

/**
 * Created by qtang on 7/2/13.
 */
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ListView;

import com.timothy.junk.R;
import com.timothy.junk.ActivityManage;
import com.timothy.junk.adapter.GFundSettingAdaptor;
import com.timothy.junk.common.parse.ParseXML;
import com.timothy.junk.common.util.SPUtil;
import com.timothy.junk.gfund.bean.GFundBean;
import com.timothy.junk.gfund.bean.GFundNotifyBean;

public class GFundSettingList extends ListActivity {
	public static final String LOG_TAG = "GFundSettingList";
	
	public static final boolean IF_NOTIFY = true;
	public static final float CEIL_DISCOUT = 2.0f;
	public static final float FLOOR_DISCOUNT = -3.0f;
	

	SharedPreferences sp;
    Context mContext;
    GFundSettingAdaptor adaptor ;
    
    List<GFundBean> gFundList;
    List<GFundNotifyBean> gFundNotifyList = new ArrayList<GFundNotifyBean>();
    
    ListView listView;
    
    public void initialData(){
    	for(GFundBean bean:gFundList){
    		GFundNotifyBean sBean = new GFundNotifyBean();
    		sBean.setIndex(bean.getIndex());
    		sBean.setFundMName(bean.getFundMName());
    		
    		String spValue = sp.getString(bean.getIndex(), null);
    		if(spValue!=null){
				  String[] spArray = spValue.split(":");
				  int notify = Integer.valueOf(spArray[0]);
				  float sCeil = Float.valueOf(spArray[1]);
				  float sFloor = Float.valueOf(spArray[2]);
				  sBean.setNotified(notify==1?true:false);
	    		  sBean.setCeilDiscount(sCeil);
	    		  sBean.setFloorDiscount(sFloor);
    		}else{
        		sBean.setCeilDiscount(CEIL_DISCOUT);
        		sBean.setFloorDiscount(FLOOR_DISCOUNT);
        		sBean.setNotified(IF_NOTIFY);
    		}

    		gFundNotifyList.add(sBean);
    	}
    }
    
    public void saveNotifyData(){
    	for(GFundNotifyBean sBean: gFundNotifyList){
    		SPUtil.save2SP(sBean.getIndex(),sBean.toSPString(), sp);     		
    	}
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gfund_setting_list);
        mContext = getApplicationContext();
        ActivityManage.add(this);
        sp = this.getSharedPreferences("GFundApp",MODE_PRIVATE);
        
        gFundList = ParseXML.getGFundList(mContext);
        
        initialData();
        
        saveNotifyData();
        
        adaptor = new GFundSettingAdaptor(mContext, gFundNotifyList,sp); 
        setListAdapter(adaptor);
        
        SPUtil.printDataInSP(sp);
    }
    

}
